package com.sanpu.kxe.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.utils.ScreenUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：退货进度
 * Created by 薛勇军 on 2018/9/18.
 */

public class ReturnProgressView extends LinearLayout {

    @Bind(R.id.initIv)
    ImageView initIv;
    @Bind(R.id.returnCheckIv)
    ImageView returnCheckIv;
    @Bind(R.id.receivedIv)
    ImageView receivedIv;
    @Bind(R.id.refundedIv)
    ImageView refundedIv;
    @Bind(R.id.successIv)
    ImageView successIv;

    @Bind(R.id.returnCheckTv)
    TextView returnCheckTv;
    @Bind(R.id.receivedTv)
    TextView receivedTv;
    @Bind(R.id.refundedTv)
    TextView refundedTv;
    @Bind(R.id.successTv)
    TextView successTv;

    @Bind(R.id.firstView)
    View firstView;
    @Bind(R.id.secondView)
    View secondView;
    @Bind(R.id.thirdView)
    View thirdView;
    @Bind(R.id.fourthView)
    View fourthView;

    private String returnProgress;//退货进度
    private boolean isFail = false;//是否失败

    public ReturnProgressView(Context context) {
        super(context);
        initView(context);
    }

    public ReturnProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ReturnProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View mView = View.inflate(context, R.layout.view_return_progress, this);
        if (mView != null) {
            ButterKnife.bind(mView);
        }
    }

    public void showReturnProgress(String returnProgress, boolean isFail) {
        this.returnProgress = returnProgress;
        this.isFail = isFail;
        showDataView();
    }

    private void showDataView() {
        switch (returnProgress) {
            case "INIT":
                setReachImage(initIv);
                break;
            case "RETURNCHECK":
                setReachImage(returnCheckIv);
                initIv.setImageResource(R.drawable.ic_return_progress_complete);
                firstView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                returnCheckTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                break;
            case "RECEIVED":
                setReachImage(receivedIv);
                initIv.setImageResource(R.drawable.ic_return_progress_complete);
                firstView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                returnCheckIv.setImageResource(R.drawable.ic_return_progress_complete);
                secondView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                returnCheckTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                receivedTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                break;
            case "REFUNDED":
                setReachImage(refundedIv);
                initIv.setImageResource(R.drawable.ic_return_progress_complete);
                firstView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                returnCheckIv.setImageResource(R.drawable.ic_return_progress_complete);
                secondView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                receivedIv.setImageResource(R.drawable.ic_return_progress_complete);
                thirdView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                returnCheckTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                receivedTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                refundedTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                break;
            case "SUCCESS":
                setReachImage(successIv);
                initIv.setImageResource(R.drawable.ic_return_progress_complete);
                firstView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                returnCheckIv.setImageResource(R.drawable.ic_return_progress_complete);
                secondView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                receivedIv.setImageResource(R.drawable.ic_return_progress_complete);
                thirdView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                refundedIv.setImageResource(R.drawable.ic_return_progress_complete);
                fourthView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.color_base));
                returnCheckTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                receivedTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                refundedTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                successTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_4a4a4a));
                break;
        }
    }

    /**
     * 设置当前终点图标
     */
    private void setReachImage(ImageView imageView) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        params.width = ScreenUtil.dp2px(13);
        params.height = ScreenUtil.dp2px(13);
        params.setMargins(0, 0, 0, ScreenUtil.dp2px(2));
        imageView.setLayoutParams(params);
        if (isFail) {
            imageView.setImageResource(R.drawable.ic_return_progress_over);
        } else {
            imageView.setImageResource(R.drawable.ic_return_progress_reach);
        }
    }
}
